﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using Dynamic.Facades.EntLib.Data;
using System.Data;

namespace Dynamic.Testing.Web
{
    public partial class GetInfo : System.Web.UI.Page
    {
        Database db = DatabaseFactory.CreateDatabase();
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = Request.QueryString["t"];
            switch (type)
            {
                case "1":
                    Response.Write(GetPINum());
                    break;
                case "2":
                    Response.Write(GetAddrs());
                    break;
                case "3":
                    string co = Request.QueryString["co"];
                    Response.Write(GetAddr(co));
                    break;
                case "4":
                    Response.Write(GetDeliveryAddrs());
                    break;
                case "5":
                    Response.Write(GetAttn());
                    break;
                case "6":
                    string id = Request.QueryString["id"];
                    Response.Write(GetDeliveryAddr(id));
                    break;
                        
            }

        }

        public string GetDeliveryAddr(string id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            SqlCommand sqlCom = new SqlCommand();
            sqlCom.CommandType = CommandType.Text;
            sqlCom.CommandText = "select Address from dbo.Address where  Comments ='" + id + "'";
            return db.ExecuteScalar(sqlCom).ToString();
        }

        public string GetDeliveryAddrs()
        {
            Database db = DatabaseFactory.CreateDatabase();
            SqlCommand sqlCom = new SqlCommand();
            sqlCom.CommandType = CommandType.Text;

            sqlCom.CommandText = "select * from dbo.Address where Comments is not null";

            DataSet dsAddr = db.ExecuteDataSet(sqlCom);
            string s = "";
            foreach (DataRow dr in dsAddr.Tables[0].Rows)
            {
                if (dr["Comments"] == null)
                {
                    continue;
                }
                if (s.Length > 0)
                {
                    s += ",";
                }
                string comm = dr["Comments"].ToString();
                string ss = comm;
                s += ss;

            }

            return s;
        }


        public string GetAddr(string id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            SqlCommand sqlCom = new SqlCommand();
            sqlCom.CommandType = CommandType.Text;
            sqlCom.CommandText = "select content from dbo.Customer where Customer='" + id + "'";

            return db.ExecuteScalar(sqlCom).ToString();
        }

        public string GetAttn()
        {
            Database db = DatabaseFactory.CreateDatabase();
            SqlCommand sqlCom = new SqlCommand();
            sqlCom.CommandText = "select * from dbo.SAO";
            DataSet dsAttn = db.ExecuteDataSet(sqlCom);
            string s = "";
            foreach (DataRow dr in dsAttn.Tables[0].Rows)
            {
                if (dr["SAO"] == DBNull.Value)
                {
                    continue;
                }
                if (s.Length > 0)
                {
                    s += ",";
                }
                s += dr["SAO"].ToString();
            }
            return s;
        }

        public string GetAddrs()
        {
            Database db = DatabaseFactory.CreateDatabase();
            SqlCommand sqlCom = new SqlCommand();
            sqlCom.CommandType = CommandType.Text;
            sqlCom.CommandText = "select * from dbo.Customer where Customer is not null and content is not null";


            DataSet dsCustom = db.ExecuteDataSet(sqlCom);
            string s = "";

            foreach (DataRow dr in dsCustom.Tables[0].Rows)
            {
                if (dr["Customer"] == DBNull.Value || dr["content"] == DBNull.Value)
                {
                    continue;
                }
                if (s.Length > 0)
                {
                    s += ",";
                }
                s += dr["Customer"].ToString();
            }

            return s;

        }

        public string GetPINum()
        {
            SqlCommand sqlCom = new SqlCommand();
            sqlCom.CommandText = "select * from dbo.v_getyear";
            sqlCom.CommandType = CommandType.Text;
            string year = db.ExecuteScalar(sqlCom).ToString();
            sqlCom.CommandText = "select * from dbo.v_getnumber";
            string number = db.ExecuteScalar(sqlCom).ToString();
            return string.Format("{0}SNA{1}SEB", year, number);
        }
    }
}
